<template>
  <section>
    <!--工具条：关键字查询 + 新增-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">关键字查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" disabled @click="handleAdd">新增</el-button>
        </el-form-item>

      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="permissions" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60" label="序号" >
      </el-table-column>
      <el-table-column prop="name" label="名称" width="150" sortable>
      </el-table-column>
      <el-table-column prop="url" label="请求地址" width="200" sortable>
      </el-table-column>
      <el-table-column prop="descs" label="描述" width="120" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="端口:方法" width="200" sortable>
      </el-table-column>


      <el-table-column label="操作" width="250">
        <template scope="scope">
          <el-button size="small" type="warning" disabled @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="small" type="danger"  disabled @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger"  @click="batchRemove" :disabled="this.sels.length===0" >批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径" prop="component">
          <el-input v-model="saveForm.component" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="访问URL" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="排序"prop="index">
            <el-input-number v-model="saveForm.index"  :min="1" :max="10" ></el-input-number>
        </el-form-item>

        <el-form-item label="简介" prop="intro">
          <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="true" aria-checked="true">正常</el-radio>
            <el-radio class="radio" :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="上级部门">
          <!--
              el-cascader - 级联框
              v-model="saveForm.parent_id" - 双向绑定

                label: 'name', - 回显哪一个属性值
                  value: 'id' - 传值哪一个属性值
              回显机制【修改】：
                1.定位：v-model="saveForm.parent_id" 与 deptTree中的每一个对象的id 定位
                2.显示哪一个属性值：label: 'name',  回显对象的name属性值
            -->
          <el-select v-model="saveForm.parentId" placeholder="请选择部门">
            <el-option v-for="item in managers"
                       :label="item.name"
                       :value="item.id">
              <span style="float: left">{{ item.name }}</span>

            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {

      //2.分页显示所属数据
      permissions: [],
      listLoading: false,
      currentPage: 1,
      pageSize: 5,
      totals: 0,

      //3.高级查询所需数据
      keyword: null,

      //4.多条数据选中
      sels: [],//列表选中列

      //5.添加或修改数据
      saveFormVisible: false,//添加或修改界面是否显示
      saveLoading: false,
      title: null,
      admins: [],
      saveFormRules: {
        permissionname: [
          { required: true, message: '请输入用户姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入用户电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入用户邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],

        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' }
        ],
        headImg: [
          { required: true, message: '请输入头像', trigger: 'blur' }
        ],
        logininfoId: [
          { required: true, message: '请输入登录信息', trigger: 'blur' }
        ],

      },
      //6.编辑界面数据
      saveForm: {
        id:null,
        permissionname:'',
        phone:'',
        email:'',
        salt:'',
        password:'',
        state:null,
        age:null,
        createtime: Date(),
        headImg:'',
        logininfo_id:null,
      },
      //7.显示所有的部门
      managers: [],

      //8.编辑界面数据
      permissionAuditLog: {
        permissionId: null,
        note:''
      }
    }
  },
  methods: {
    //1.获取列表
    getPermissions(){
      let paras = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;
      this.$http.post("/permission",paras).then(res=>{
        this.totals = res.data.totals;
        this.permissions = res.data.data;
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("获取分页数据失败!!!");
      })
    },


    //4.关键字查询
    keywordQuery(){
      this.currentPage = 1;
      this.getPermissions();
    },

    //5.分页查询 - 点击某一页查询
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPermissions();
    },

    //6.删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/permission/" + row.id).then(res => {
          this.listLoading = false;
          if(res.data.success){
            //计算总页数
            let totalPage = this.totals%this.pageSize==0?this.totals/this.pageSize
                :Math.ceil(this.totals/this.pageSize);
            if(this.currentPage > 1 && this.currentPage==totalPage && (this.totals-1)%this.pageSize==0){
              this.currentPage = this.currentPage - 1;
            }
            this.getPermissions();
          }else{
            this.$message.error("删除失败【500】");
          }
        }).catch(res => {
          this.$message.error("系统错误【400,404】");
        })
      }).catch(() => {
        //确认框点击取消之后走这里
      });
    },

    //7.点击修改按钮弹出模态框
    handleEdit: function (index, row) {
      this.title = "编辑";
      this.saveForm = Object.assign({}, row);


      this.saveFormVisible = true;
      //10.获取所有的经理
      this.getManagers();

    },

    //8.点击添加按钮弹出模态框
    handleAdd: function () {
      this.title = "添加";
      this.saveForm = {
        id:null,
        permissionname:'',
        phone:'',
        email:'',
        salt:'',
        password:'',
        state:null,
        age:null,
        createtime: Date(),
        headImg:'',
        logininfo_id:null,
      };

      this.saveFormVisible = true;
      //10.获取所有的经理
      this.getManagers();
    },

    //10.点击模态框提交数据
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let paras = Object.assign({}, this.saveForm);
            this.$http.put("/permission",paras).then(res => {
              this.saveFormVisible = false;
              this.saveLoading = false;
              if(res.data.success){
                this.getPermissions();
              }else{
                this.$message.error("操作失败【500】");
              }
            }).catch(res => {
              this.$message.error("系统错误【400,404】");
            })
          });
        }
      });
    },
    //10.获取所有的部门
    getManagers(){
      this.$http.get("/permission").then(res=>{
        this.managers = res.data;
      })
    },
    //11.批量操作获取选中的数据
    selsChange: function (sels) {
      this.sels = sels;
    },
    //12.批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/permission",ids).then(res => {
          this.listLoading = false;
          if(res.data.success){
            this.currentPage = 1;
            this.getPermissions();
          }else{
            this.$message.error("批量删除失败【500】");
          }
        }).catch(res => {
          this.$message.error("系统错误【400,404】");
        })
      }).catch(() => {
      });
    },

  },
  mounted() {
    this.getPermissions();
  }
}

</script>

<style scoped>

</style>